package com.lizhangjun.controller;


import com.lizhangjun.domain.QUser;
import com.lizhangjun.entity.Result;
import com.lizhangjun.service.QUserService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.web.bind.annotation.*;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author lizhangjun
 * @since 2021-06-23
 */
@Api(tags = {"用户相关接口"} )
@RestController
@RequiredArgsConstructor
@RequestMapping("/q-user")
public class QUserController {

    private final QUserService userService;

    private final BCryptPasswordEncoder bCryptPasswordEncoder;

    /**
     * 添加用户
     * @param user 用户信息
     * @return 是否添加成功
     */
    @ApiOperation("添加用户")
    @PostMapping("/add")
    public Result add(@RequestBody QUser user){
        if(userService.saveOrUpdate(user)){
            return Result.success();
        }else{
            return Result.fail("添加用户失败");
        }
    }

    /**
     * 用户信息
     * @param id 用户id
     * @return 用户信息
     */
    @ApiOperation("根据id查找用户信息")
    @GetMapping("/userinfo/{id}")
    public Result userInfo(@PathVariable Integer id){
        return Result.success(userService.getById(id));
    }

    /**
     * 查找所有
     * @return 查找所有用户
     */
    @ApiOperation("查找所有用户")
    @GetMapping("/list")
    public Result list(){
        return Result.success(userService.list());
    }
}

